package com.laxia.codegenerator.mybatis;

import com.laxia.util.StringUtil;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 自动生成数据库映射文件
 *
 * @author weishuai
 */
public class MybatisGenerator {

    public static void generator(String configName, String contextId, List<String> tableNames) {
        try {
            String resourcePath = System.getProperty("user.dir") + "/gen/src/main/resources";
            String javaPath = System.getProperty("user.dir") + "/gen/src/main/java";

            File directoryResource = new File(resourcePath);
            File directoryJava = new File(javaPath);
            if (directoryResource.exists()) {
                deleteDir(resourcePath);
            }
            if (directoryJava.exists()) {
                deleteDir(javaPath);
            }

            boolean mkdir = directoryResource.mkdirs();
            System.out.println("create resource dir:" + mkdir);

            mkdir = directoryJava.mkdirs();
            System.out.println("create java dir:" + mkdir);

            List<String> warnings = new ArrayList<>();
            URL url = MybatisGenerator.class.getClassLoader().getResource(configName);
            if (Objects.isNull(url)) {
                System.err.println(configName + " not exists");
                return;
            }
            String configFilePath = url.getPath();
            System.out.println("加载配置文件===" + configFilePath);
            File configFile = new File(configFilePath);
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(true);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

            if (StringUtil.isNotBlank(contextId)) {
                HashSet<String> contextIds = new HashSet<>();
                contextIds.add(contextId);
                myBatisGenerator.generate(null, contextIds, new HashSet<>(tableNames));
            } else {
                myBatisGenerator.generate(null);
            }
            warnings.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static boolean deleteDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }
        String[] content = file.list();
        Optional.ofNullable(content).map(Arrays::stream).ifPresent(list -> list.forEach(name -> {
            File temp = new File(path, name);
            if (temp.isDirectory()) {
                deleteDir(temp.getAbsolutePath());
            }
            System.out.println(String.format("file [%s] delete: %s", temp.getPath(), temp.delete()));
        }));
        return true;
    }
}
